package com.zsc.shixun.service.impl;

import com.zsc.shixun.common.ResultCode;
import com.zsc.shixun.contant.Contant;
import com.zsc.shixun.controller.WebSocketServer;
import com.zsc.shixun.entity.Notice;
import com.zsc.shixun.entity.User;
import com.zsc.shixun.exception.ApiException;
import com.zsc.shixun.mapper.NoticeMapper;
import com.zsc.shixun.model.notice.NoticeCreateVO;
import com.zsc.shixun.model.notice.NoticeMapping;
import com.zsc.shixun.model.notice.NoticeUpdateVO;
import com.zsc.shixun.model.notice.NoticeVO;
import com.zsc.shixun.service.IMessageService;
import com.zsc.shixun.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-07
 */
@Service
@Transactional
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    private static final Logger logger = LoggerFactory.getLogger(NoticeServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private NoticeMapping noticeMapping;
    @Autowired
    private IMessageService messageService;

    @Override
    public List<NoticeVO> getAll() {
        if (redisTemplate.hasKey(Contant.NOTIC_KEY)) {
            return (List<NoticeVO>) redisTemplate.opsForValue().get(Contant.NOTIC_KEY);
        }
        List<Notice> noticeList = this.list();
        List<NoticeVO> noticeVOS = noticeMapping.entitysToVos(noticeList);

        redisTemplate.opsForValue().set(Contant.NOTIC_KEY, noticeVOS);
        return noticeVOS;
    }

    @Override
    public void update(NoticeUpdateVO noticeUpdateVO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateVO, notice);
        try {
            this.updateById(notice);
        } catch (Exception e) {
            logger.error("修改公告内容失败");
            throw new ApiException(ResultCode.NOTICE_UPDATE_FAIL);
        }
        logger.info("修改公告内容成功");
        if (redisTemplate.hasKey(Contant.NOTIC_KEY)) {
            redisTemplate.delete(Contant.NOTIC_KEY);
        }
    }

    @Override
    public void insert(NoticeCreateVO noticeCreateVO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeCreateVO, notice);
        try {
            this.save(notice);
        } catch (Exception e) {
            logger.error("插入公告内容失败");
            throw new ApiException(ResultCode.NOTICE_INSERT_FAIL);
        }
        logger.info("插入公告内容成功");
        if (redisTemplate.hasKey(Contant.NOTIC_KEY)) {
            redisTemplate.delete(Contant.NOTIC_KEY);
        }
        if(redisTemplate.hasKey(Contant.INDEX_NOTICE_KEY))
        {
            redisTemplate.delete(Contant.INDEX_NOTICE_KEY);
        }
//        User sender=userService.getById(uid);

        String message="最新公告："+noticeCreateVO.getContent();
        ;
        try {
            WebSocketServer.sendObj(messageService.insertMessage(null,null,message));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(Long id) {
        try {
            this.removeById(id);
        } catch (Exception e) {
            logger.error("删除公告内容失败");
            throw new ApiException(ResultCode.NOTICE_DELETE_FAIL);
        }
        logger.info("删除公告内容成功");

        if (redisTemplate.hasKey(Contant.NOTIC_KEY)) {
            redisTemplate.delete(Contant.NOTIC_KEY);
        }
    }
}
